﻿/// <reference path="Silverlight.js" />
/// <reference path="MicrosoftAjax.js" />

// variable to allow the asmx file to be easily hosted in a different location
var servicePath = "/Services/PageMappingsService.asmx";

// Begin Service Proxy //
Type.registerNamespace('OpenLightGroup');
OpenLightGroup.PageMappingsService = function() {
    OpenLightGroup.PageMappingsService.initializeBase(this);
    this._timeout = 0;
    this._userContext = null;
    this._succeeded = null;
    this._failed = null;
}
OpenLightGroup.PageMappingsService.prototype = {
    _get_path: function() {
        var p = this.get_path();
        if (p) return p;
        else return OpenLightGroup.PageMappingsService._staticInstance.get_path();
    },
    GetHtmlUrl: function(silverlightUrl, succeededCallback, failedCallback, userContext) {
        return this._invoke(this._get_path(), 'GetHtmlUrl', false, { silverlightUrl: silverlightUrl }, succeededCallback, failedCallback, userContext);
    },
    GetSilverlightUrl: function(htmlUrl, succeededCallback, failedCallback, userContext) {
        return this._invoke(this._get_path(), 'GetSilverlightUrl', false, { htmlUrl: htmlUrl }, succeededCallback, failedCallback, userContext);
    } 
}
OpenLightGroup.PageMappingsService.registerClass('OpenLightGroup.PageMappingsService', Sys.Net.WebServiceProxy);
OpenLightGroup.PageMappingsService._staticInstance = new OpenLightGroup.PageMappingsService();
OpenLightGroup.PageMappingsService.set_path = function(value) { OpenLightGroup.PageMappingsService._staticInstance.set_path(value); }
OpenLightGroup.PageMappingsService.get_path = function() { return OpenLightGroup.PageMappingsService._staticInstance.get_path(); }
OpenLightGroup.PageMappingsService.set_timeout = function(value) { OpenLightGroup.PageMappingsService._staticInstance.set_timeout(value); }
OpenLightGroup.PageMappingsService.get_timeout = function() { return OpenLightGroup.PageMappingsService._staticInstance.get_timeout(); }
OpenLightGroup.PageMappingsService.set_defaultUserContext = function(value) { OpenLightGroup.PageMappingsService._staticInstance.set_defaultUserContext(value); }
OpenLightGroup.PageMappingsService.get_defaultUserContext = function() { return OpenLightGroup.PageMappingsService._staticInstance.get_defaultUserContext(); }
OpenLightGroup.PageMappingsService.set_defaultSucceededCallback = function(value) { OpenLightGroup.PageMappingsService._staticInstance.set_defaultSucceededCallback(value); }
OpenLightGroup.PageMappingsService.get_defaultSucceededCallback = function() { return OpenLightGroup.PageMappingsService._staticInstance.get_defaultSucceededCallback(); }
OpenLightGroup.PageMappingsService.set_defaultFailedCallback = function(value) { OpenLightGroup.PageMappingsService._staticInstance.set_defaultFailedCallback(value); }
OpenLightGroup.PageMappingsService.get_defaultFailedCallback = function() { return OpenLightGroup.PageMappingsService._staticInstance.get_defaultFailedCallback(); }
OpenLightGroup.PageMappingsService.set_path(servicePath);
OpenLightGroup.PageMappingsService.GetHtmlUrl = function(silverlightUrl, onSuccess, onFailed, userContext) { OpenLightGroup.PageMappingsService._staticInstance.GetHtmlUrl(silverlightUrl, onSuccess, onFailed, userContext); }
OpenLightGroup.PageMappingsService.GetSilverlightUrl = function(htmlUrl, onSuccess, onFailed, userContext) { OpenLightGroup.PageMappingsService._staticInstance.GetSilverlightUrl(htmlUrl, onSuccess, onFailed, userContext); }

// End Service Proxy //


//Call this function from the page hosting the Silverlight application
function RedirectToAsp() {

    if (!Silverlight.isInstalled(null)) {
        //If silverlight is not installed
        // request the corresponding ASP page 
        // configured in the mappings.

        OpenLightGroup.PageMappingsService.GetHtmlUrl(document.URL, Redirect, ServiceFailed, null);
        
    }
}

//Call this function from the head section each page of the HTML site 
//    (if using a master page, place it in the head section)
function RedirectToSilverlight() {

    if (Silverlight.isInstalled(null)) {
        //If silverlight is installed
        // request the corresponding Silverlight link
        // configured in the mappings.

        OpenLightGroup.PageMappingsService.GetSilverlightUrl(document.URL, Redirect, ServiceFailed, null);        
              
    }
}

// Callback function, that does the actual redirect if a URL is returned
function Redirect(newUrl) {

    if (newUrl != '') {
        document.location = newUrl;
    }
}

//Call this function from the page hosting the Silverlight application
function UpdateHtmlLink(link) {

    OpenLightGroup.PageMappingsService.GetHtmlUrl(document.URL, SetHref, ServiceFailed, link);

}

//Updates the href attribute of the anchor element to point to the 
// corresponding page.
function SetHref(newUrl, link) {

    link.href = newUrl;
    
}

// Used to handle failed calls to the mappings service
function ServiceFailed(err) {

}



